package mini.hug;

import java.util.ArrayList;

import audio.Audio;
import audio.Clip;
import owg.util.Calc;
import owg.util.opengl.ColorF;
import hubol.Sprite;

public class Understanding extends Bogus{
	private final Shake s = Hug.me.shake(), t = Hug.me.shake();
	private final Clip flap = Audio.getClip("flap"), understanding = Audio.getClip("understanding");
	private double alpha = 0;
	
	private final Sprite title = new Sprite("title"), birgus = new Sprite("birgus");
	private ArrayList<Mini> birguses;
	private boolean wtf = false;

	public Understanding(float xsc) {
		super(Hug.me.bogus.x, Hug.me.bogus.y, 3);
		Hug.me.bogus = this;
		
		xsc *= -1;
		
		flap.loop();
		Hug.me.stop();
		understanding.play();
		
		sprite = new Sprite("understanding");
		hspeed = -1 * xsc;
		vspeed = 5;
		
		xscale = xsc;
		
		imageSpeed = .125f;
		
		setDepth(-900);
		
		birguses = new ArrayList<Mini>();
		birguses.add(new Mini(-100, -100, 75, 67));
		birguses.add(new Mini(200, 550, 250, 425));
		birguses.add(new Mini(930, 250, 785, 230));
		
		wtf = true;
		alarm[0] = 180 + 45;
		alarm[1] = 195 + 46;
		alarm[2] = 215 + 48;
	}
	
	public void alarmEvent(int i){
		if (wtf)
			birguses.get(i).approach = true;
	}
	
	public void step(){
		if (wtf){
			hspeed = Calc.approach(hspeed, -2 * xscale, 20);
			vspeed = Calc.approach(vspeed, -8, 160);
			
			if (vspeed < 2)
				new Exhaust(x + 10, y - 40, xscale);
			
			if (vspeed < -6){
				alpha += .00088;
				if (alpha > 1.5)
					Hug.me.completeMinigame();
			}
			
			if (sprite != null){
				imageSingle += imageSpeed;
				while(imageSingle >= sprite.texture.length)
					imageSingle -= sprite.texture.length;
			}
			
			imageSpeed = (float)Calc.approach(imageSpeed, .075, 20);
			
			x += hspeed;
			if (vspeed < 0)
				y += vspeed;
			
			if (birguses != null){
				for (Mini m : birguses)
					m.step();
			}
			
			alarmStep();
		}
	}
	
	private class Mini{
		public double img;
		public boolean approach;
		
		public double x, y, goalX, goalY;
		private boolean ok = false;
		private Shake s = Hug.me.shake();
		
		private double a = Calc.random(.1), b = Calc.random(.1), c = 0;
		
		public Mini(double x, double y, double goalX, double goalY){
			this.x = x;
			this.y = y;
			this.goalX = goalX;
			this.goalY = goalY;
			img = Calc.random(2);
		}
		
		public void step(){
			if (approach){
				x = Calc.approach(x + (Math.sin(c * a) * 2), goalX, 25);
				y = Calc.approach(y + (Math.cos(c * b) * 2), goalY, 25);
				if (ok)
					new Exhaust(Hug.me.viewX + x, Hug.me.viewY + y, xscale);
				ok = !ok;
				c++;
			}
		}
		
		public void render(){
			birgus.render((imageSingle + img) % 2.0, Hug.me.viewX + x + s.x, Hug.me.viewY + y + s.y, .25f * xscale, .25f, (float)(s.z / 3.0), ColorF.WHITE);
		}
	}
	
	public void render(){
		if (wtf){
			if (birguses != null){
				for (Mini m : birguses)
					m.render();
			}
			sprite.render(imageSingle, x + s.x, y + s.y - 50, .25f * xscale, .25f, (float)(s.z / 14.0), ColorF.WHITE);
			title.render(0, Hug.me.viewX + t.x - 432 + (864 * Math.min(1, alpha)), Hug.me.viewY + t.y + 343, .42f, .42f, (float)(t.z / 22.0), ColorF.WHITE);
		}
	}

}
